<template>
  <div class="dailog" v-show="isVisible" @click.self="close">
    <div class="dailog-wrap">
      <div class="dialog-header">
        <p class="dialog-header-title"><slot name="header" /></p>
        <button class="btn" @click="close">X</button>
      </div>
      <div class="dialog-body">
        <slot />
      </div>
      <div class="dialog-footer">
        <slot name="footer" />
      </div>
    </div>
  </div>
</template>

<script>
export default {
  name: 'Dialog',
  props: {
    isVisible: {
      type: Boolean,
      required: true,
    },
  },
  methods: {
    close() {
      this.$emit('update:isVisible', false)
    },
  },
  watch: {
    isVisible: {
      handler(isVisible) {
        if (isVisible) {
          // 显示弹框时,禁用滚动条
          document.documentElement.style.overflow = 'hidden'
        } else {
          // 弹框消失时,滚动条恢复
          document.documentElement.style.overflow = 'auto'
        }
      },
      immediate: true,
    },
  },
}
</script>

<style lang="less" scoped>
.dailog {
  position: fixed;
  left: 0;
  right: 0;
  top: 0;
  bottom: 0;
  background-color: rgba(0, 0, 0, 0.3);
  .dailog-wrap {
    position: absolute;
    left: 50%;
    top: 50%;
    transform: translate(-50%, -50%);
    width: 400px;
    padding: 10px 20px;
    box-sizing: border-box;
    border-radius: 3px;
    background-color: #fff;
  }
}
.dialog-header {
  display: flex;
  justify-content: space-between;
  .dialog-header-title {
    font-size: 18px;
  }
  .btn {
    background-color: transparent;
    border: none;
    &:hover {
      color: red;
    }
  }
}
.dialog-body {
  font-size: 14px;
  padding: 20px 0;
}
.dialog-footer {
  float: right;
}
</style>
